package com.yhj.service.impl;

import com.yhj.model.SecurityUser;
import com.yhj.model.User;
import com.yhj.model.UserRole;
import com.yhj.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author yaohj
 * @date 2024年12月11日 11:09
 */
@Service
public class MyUserDetailService implements UserDetailsService {
    @Resource
    private UserService userService;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);
        if(user == null) {
            throw new UsernameNotFoundException(username);
        }
        //这里实际做的是数据库查询用户权限
        UserRole userRole= new UserRole();
        userRole.setId(1);
        userRole.setUserId(user.getId());
        userRole.setRoleId(1);
        userRole.setRoleName("ADMIN");
        user.setUserRoles(Collections.singletonList(userRole));

        SecurityUser securityUser= new SecurityUser();
        securityUser.isAccountNonLocked();
        securityUser.setUser(user);
        return securityUser;
    }

   /* @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }*/
}
